﻿Public Class EnglishCj
    Private _type As Integer = 0
    Private _word As String
    Private _单词意思 As Windows.Forms.Control
    Private _句子 As Windows.Forms.Control
    Private _单词1 As Windows.Forms.Control
    Private _英国音标 As Windows.Forms.Control
    Private _美国音标 As Windows.Forms.Control
    Private _mode As Integer = 0
    Private _bWebBrowser1 As Boolean = False
    Private _bWebBrowser2 As Boolean = False
    Private Sub WebBrowser1_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        If WebBrowser1.ReadyState = Windows.Forms.WebBrowserReadyState.Complete Then
            _bWebBrowser1 = True
        End If

    End Sub

    Public Sub New()

        ' 此调用是 Windows 窗体设计器所必需的。
        InitializeComponent()
        ' 在 InitializeComponent() 调用之后添加任何初始化。

    End Sub

    Public Sub LoadContent(ByVal type As Integer, _
                           ByVal word As String, _
                           ByVal 单词 As System.Windows.Forms.Control, _
                           ByVal 单词意思 As System.Windows.Forms.Control, _
                           ByVal 句子 As System.Windows.Forms.Control, _
                           ByVal 英国音标 As System.Windows.Forms.Control, _
                           ByVal 美国音标 As System.Windows.Forms.Control, _
                           Optional ByVal mode As Integer = 1)
        _type = type
        _word = word
        _单词意思 = 单词意思
        _句子 = 句子
        _单词1 = 单词
        _英国音标 = 英国音标
        _美国音标 = 美国音标
        _mode = mode
        If _type = 1 Then
            WebBrowser1.Navigate("http://dict.cn/mini.php?q=" + _word.Trim())
            WebBrowser2.Navigate(String.Format("http://dict.baidu.com/s?wd={0}", _word.Trim()))
        ElseIf _type = 0 Then '沪江字典
            WebBrowser1.Navigate("http://dict.hjenglish.com/w/" + _word.Trim() + "+")
            WebBrowser2.Navigate(String.Format("http://dict.baidu.com/s?wd={0}", _word.Trim()))
            _type = 0
        End If
    End Sub

    Private Sub MyButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyButton3.Click
        If _type = 0 Then
            Dim he As System.Windows.Forms.HtmlElement = WebBrowser1.Document.GetElementById("panel_websentence_content")
            If MyCheckBox2.Checked Then
                If he IsNot Nothing Then
                    Dim str As String = he.InnerText.Replace("more...", " ")
                    str = str.Replace("不适合", " ")
                    str = str.Replace("收藏", " ")
                    _句子.Text = str.Trim()
                End If
            End If

            If 单词.Checked Then
                he = WebBrowser1.Document.GetElementById("_word")
                If he IsNot Nothing Then
                    _单词1.Text = he.GetAttribute("Value")
                End If
            End If

            If MyCheckBox3.Checked Then
                he = WebBrowser1.Document.GetElementById("panel_comment")
                If he IsNot Nothing Then
                    _单词意思.Text = he.InnerText
                End If
            End If

            If chk音标.Checked Then
                Dim hes As System.Windows.Forms.HtmlElementCollection = WebBrowser2.Document.GetElementsByTagName("span")
                If hes.Count > 0 Then
                    Dim j As Integer = 0
                    For i As Integer = 0 To hes.Count - 1
                        If hes(i).GetAttribute("classname") = "yinbiao" Then
                            If j = 0 Then
                                _英国音标.Text = hes(i).InnerText
                                j = 1
                            ElseIf j = 1 Then
                                _美国音标.Text = hes(i).InnerText
                                j = 2
                            End If
                        End If
                        If j = 2 Then
                            Exit For
                        End If
                    Next
                End If
            End If
        Else

            Dim hes As System.Windows.Forms.HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("div")
            If hes.Count > 0 Then
                If MyCheckBox2.Checked Then
                    Dim str As String = hes.Item(1).InnerText
                    If str.StartsWith("[ 自动跳转自: ") Then
                        If hes.Count > 2 Then
                            str = hes(2).InnerText
                        End If
                    End If
                    _单词意思.Text = str.Trim()
                End If

                If 单词.Checked Then
                    Dim hes1 As System.Windows.Forms.HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("span")
                    For Each he2 As System.Windows.Forms.HtmlElement In hes1
                        If he2.GetAttribute("class") = "k" Then
                            _单词1.Text = hes(1).InnerText
                            Exit For
                        End If
                    Next
                End If

                If MyCheckBox3.Checked Then
                    Dim sr As String = ""
                    Dim str As String = ""
                    If hes.Count > 1 Then
                        str = hes.Item(1).InnerText
                    End If
                    If hes.Count > 3 Then
                        sr = hes.Item(3).InnerText
                    End If

                    If str.StartsWith("[ 自动跳转自: ") Then
                        If hes.Count > 4 Then
                            sr = hes(4).InnerText
                        End If
                    End If
                    Dim lst As New List(Of String)
                    For Each str1 As String In sr.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
                        If str1.StartsWith("1.") Or str1.StartsWith("2.") Or str1.StartsWith("4.") Or str1.StartsWith("3.") Or str1.StartsWith("5.") _
                        Or str1.StartsWith("6.") Or str1.StartsWith("7.") Or str1.StartsWith("8.") Or str1.StartsWith("9.") Then
                            lst.Add(str1.Substring(2).Trim())
                        Else
                            lst.Add(str1.Trim())
                        End If
                    Next
                    _句子.Text = ""
                    For Each str2 As String In lst
                        _句子.Text += str2 + Environment.NewLine
                    Next
                End If
            End If
            If chk音标.Checked Then
                hes = WebBrowser2.Document.GetElementsByTagName("span")
                If hes.Count > 0 Then
                    Dim j As Integer = 0
                    For i As Integer = 0 To hes.Count - 1
                        If hes(i).GetAttribute("classname") = "yinbiao" Then
                            If j = 0 Then
                                _英国音标.Text = hes(i).InnerText
                                j = 1
                            ElseIf j = 1 Then
                                _美国音标.Text = hes(i).InnerText
                                j = 2
                            End If
                        End If
                        If j = 2 Then
                            Exit For
                        End If
                    Next
                End If
            End If
        End If
    End Sub

    Private Sub MyButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyButton4.Click
        If _type = 0 Then
            Dim he As System.Windows.Forms.HtmlElement = WebBrowser1.Document.GetElementById("panel_websentence_content")
            If MyCheckBox3.Checked Then
                If he IsNot Nothing Then
                    Dim str As String = he.InnerText.Replace("more...", " ")
                    str = str.Replace("不适合", " ")
                    str = str.Replace("收藏", " ")
                    _句子.Text += Environment.NewLine + str.Trim()
                End If
            End If

            If 单词.Checked Then
                he = WebBrowser1.Document.GetElementById("_word")
                If he IsNot Nothing Then
                    _单词1.Text = he.GetAttribute("Value")
                End If
            End If

            If 单词.Checked Then
                Dim hes As System.Windows.Forms.HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("h1")
                For Each he2 As System.Windows.Forms.HtmlElement In hes
                    If he2.GetAttribute("class") = "h1word" Then
                        _单词1.Text = he2.InnerText()
                    End If

                Next
            End If

            If MyCheckBox2.Checked Then
                he = WebBrowser1.Document.GetElementById("panel_comment")
                If he IsNot Nothing Then
                    _单词意思.Text += Environment.NewLine + he.InnerText
                End If
            End If

            If chk音标.Checked Then
                Dim hes As System.Windows.Forms.HtmlElementCollection = WebBrowser2.Document.GetElementsByTagName("span")
                If hes.Count > 0 Then
                    Dim j As Integer = 0
                    For i As Integer = 0 To hes.Count - 1
                        If hes(i).GetAttribute("classname") = "yinbiao" Then
                            If j = 0 Then
                                _英国音标.Text = hes(i).InnerText
                                j = 1
                            ElseIf j = 1 Then
                                _美国音标.Text = hes(i).InnerText
                                j = 2
                            End If
                        End If
                        If j = 2 Then
                            Exit For
                        End If
                    Next
                End If
            End If
        Else
            Dim hes As System.Windows.Forms.HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("div")
            If hes.Count > 0 Then
                If MyCheckBox2.Checked Then
                    Dim str As String = hes.Item(1).InnerText
                    If str.StartsWith("[ 自动跳转自: ") Then
                        If hes.Count > 2 Then
                            str = hes(2).InnerText
                        End If
                    End If
                    _单词意思.Text += Environment.NewLine + str.Trim()
                End If

                If 单词.Checked Then
                    Dim hes1 As System.Windows.Forms.HtmlElementCollection = WebBrowser1.Document.GetElementsByTagName("span")
                    For Each he2 As System.Windows.Forms.HtmlElement In hes1
                        If he2.GetAttribute("class") = "k" Then
                            _单词1.Text = he2.InnerText
                            Exit For
                        End If
                    Next
                End If

                If MyCheckBox3.Checked Then
                    Dim sr As String = ""
                    Dim str As String = ""
                    If hes.Count > 1 Then
                        str = hes.Item(1).InnerText
                    End If
                    If hes.Count > 3 Then
                        sr = hes.Item(3).InnerText
                    End If
                    If str.StartsWith("[ 自动跳转自: ") Then
                        If hes.Count > 4 Then
                            sr = hes(4).InnerText
                        End If
                    End If
                    Dim lst As New List(Of String)
                    For Each str1 As String In sr.Split(New String() {Environment.NewLine}, StringSplitOptions.RemoveEmptyEntries)
                        If str1.StartsWith("1.") Or str1.StartsWith("2.") Or str1.StartsWith("4.") Or str1.StartsWith("3.") Or str1.StartsWith("5.") _
                        Or str1.StartsWith("6.") Or str1.StartsWith("7.") Or str1.StartsWith("8.") Or str1.StartsWith("9.") Then
                            lst.Add(str1.Substring(2).Trim())
                        Else
                            lst.Add(str1.Trim())
                        End If
                    Next

                    For Each str2 As String In lst
                        _句子.Text += str2 + Environment.NewLine
                    Next

                End If
            End If

            If chk音标.Checked Then
                hes = WebBrowser2.Document.GetElementsByTagName("span")
                If hes.Count > 0 Then
                    Dim j As Integer = 0
                    For i As Integer = 0 To hes.Count - 1
                        If hes(i).GetAttribute("classname") = "yinbiao" Then
                            If j = 0 Then
                                _英国音标.Text = hes(i).InnerText
                                j = 1
                            ElseIf j = 1 Then
                                _美国音标.Text = hes(i).InnerText
                                j = 2
                            End If
                        End If
                        If j = 2 Then
                            Exit For
                        End If
                    Next
                End If
            End If

        End If
    End Sub

    Private Sub 采集ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 采集ToolStripMenuItem.Click
        MyButton3_Click(sender, e)
    End Sub

    Private Sub 追加采集ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 追加采集ToolStripMenuItem.Click
        MyButton4_Click(sender, e)
    End Sub

    Private Sub 单词ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 单词ToolStripMenuItem.Click
        单词.Checked = Not 单词.Checked
    End Sub

    Private Sub 汉语意思ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 汉语意思ToolStripMenuItem.Click
        MyCheckBox2.Checked = Not MyCheckBox2.Checked
    End Sub

    Private Sub 句子ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 句子ToolStripMenuItem.Click
        MyCheckBox3.Checked = Not MyCheckBox3.Checked
    End Sub

    Private Sub WebBrowser2_DocumentCompleted(ByVal sender As System.Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser2.DocumentCompleted
        If WebBrowser2.ReadyState = Windows.Forms.WebBrowserReadyState.Complete Then
            _bWebBrowser2 = True
        End If
       
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If _bWebBrowser2 = True And _bWebBrowser1 = True Then
            If _mode <> 0 Then
                MyButton3.Enabled = True
                MyButton4.Enabled = True
                Me.Timer1.Enabled = False
            Else
                MyButton3.Enabled = False
                MyButton4.Enabled = False
            End If
        End If

    End Sub
End Class
